#include "doubly_linked_list_v2.h"
Link *Link::insert(Link *n)
{
    if (n == nullptr)
        return this;
    n->succ = this;
    if (prev)
        prev->succ = n;
    n->prev = prev;
    prev = n;
    return n;
}
Link *Link::add(Link *n)
{
    if (n == nullptr)
        return this;
    n->succ = succ;
    n->prev = this;
    if (succ)
        succ->prev = n;
    succ = n;
    return n;
}
Link *Link::erase()
{
    if (succ)
        succ->prev = prev;
    if (prev)
        prev->succ = succ;
    return succ;
}
Link *Link::find(const std::string &s)
{
    return const_cast<Link *>(static_cast<const Link *>(this)->find(s));
}
const Link *Link::find(const std::string &s) const
{
    const Link *p = this;
    while (p)
    {
        if (p->value == s)
            return p;
        p = p->succ;
    }
    return nullptr;
}
Link *Link::advance(int n)
{
    return const_cast<Link *>(static_cast<const Link *>(this)->advance(n));
}
const Link *Link::advance(int n) const
{
    const Link *p = this;
    if (n > 0)
    {
        while (n--)
        {
            if (p->succ == nullptr)
                return nullptr;
            p = p->succ;
        }
    }
    else if (n < 0)
    {
        while (n++)
        {
            if (p->prev == nullptr)
                return nullptr;
            p = p->prev;
        }
    }
    return p;
}